package com.wang.sort;
//环形数组的实现
public class Demo12_RingArray {
    public static class MyQueue {
        private int[] arr;
        private int pushIndex;
        private int pollIndex;
        private int size;
        private final int limit;

        public MyQueue(int l){
            arr = new int[l];
            pushIndex = 0;
            pollIndex = 0;
            size = 0;
            limit = l;
        }
        public void push(int value){
            if(size == limit){
                throw new RuntimeException("栈已满，不能添加元素");
            }
            size++;
            arr[pushIndex] = value;
            pushIndex = nextIndex(pushIndex);
        }
        public void poll(){
            if(size == 0){
                throw new RuntimeException("栈已为空，没有元素可以弹出");
            }
            size--;
            int ans = arr[pollIndex];
            pollIndex = nextIndex(pollIndex);

        }
        //判空
        public boolean isEmpty(){
            return size == 0;
        }
        //如果现在的下标为i，返回下一个位置
        private int nextIndex(int i) {
            return i < limit - 1 ? i + 1 : 0;
        }

    }
}
